package com.csx.micrometer.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;

import javax.annotation.PostConstruct;
import java.time.Duration;
import java.util.Arrays;

/**
 * @author cuisongxu
 * @date 2022/11/20 20:12
 */
@ConfigurationProperties(prefix = "micrometer.apm.metric")
public class MicrometerMetricProperties {

    private static MicrometerMetricProperties instance;

    /**
     * 直方图区间
     */
    private String histogramDurationMills = "50,100,200,500,1000";
    private Duration[] histogramDurations;

    public static Duration[] getHistogramDurations() {
        return instance.histogramDurations;
    }

    @PostConstruct
    public void init() {
        instance = this;
        this.initHistogramDurations(histogramDurationMills);
    }

    private void initHistogramDurations(String histogramDurationMills) {
        if (histogramDurationMills == null) {
            return;
        }
        histogramDurations = Arrays.stream(histogramDurationMills.split(",")).map(s -> {
            Long mill = Long.valueOf(s.trim());
            return Duration.ofMillis(mill);
        }).toArray(Duration[]::new);
    }
}
